using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource("CodeStory.RichWebEditor.Resource.JavaScript.MicrosoftAjax.js", "application/x-javascript")]
[assembly: WebResource("CodeStory.RichWebEditor.Resource.JavaScript.LazyLoad.js", "application/x-javascript")]
[assembly: WebResource("CodeStory.RichWebEditor.Resource.JavaScript.WebEditor.js", "application/x-javascript")]

namespace CodeStory.RichWebEditor
{
	[DefaultProperty("Text")]
	[ToolboxData("<{0}:WebEditor runat=server></{0}:WebEditor>")]
	public class WebEditor : WebControl
	{
		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public string Text {
			get {
				object obj = ViewState["Text"];
				return ((obj == null) ? String.Empty : (string)obj);
			}

			set {
				ViewState["Text"] = value;
			}
		}
		private StringBuilder _script;
		private StringBuilder lazyLoadScriptFilesScript;
		public StringBuilder Script {
			get {
				if (_script == null)
					_script = new StringBuilder();
				return _script; }
		}
		public StringBuilder LazyLoadScriptFilesScript {
			get {
				if (lazyLoadScriptFilesScript == null)
					lazyLoadScriptFilesScript = new StringBuilder();
				return lazyLoadScriptFilesScript; }
		}

		protected override void OnPreRender(EventArgs e) {
			Script.Append("var ")
			.Append(this.ClientID)
			.Append(" = new CodeStory.WebEditor();\n\r")
			.Append(this.ClientID)
			.Append(".Initialize();\n\r");

			if (Page != null) {
				Page.ClientScript.RegisterClientScriptResource(GetType(), "CodeStory.RichWebEditor.Resource.JavaScript.MicrosoftAjax.js");
				Page.ClientScript.RegisterClientScriptResource(GetType(), "CodeStory.RichWebEditor.Resource.JavaScript.LazyLoad.js");
				Page.ClientScript.RegisterClientScriptResource(GetType(), "CodeStory.RichWebEditor.Resource.JavaScript.WebEditor.js");
				Page.ClientScript.RegisterStartupScript(GetType(), this.ClientID, Script.ToString(),true);
				Page.ClientScript.RegisterClientScriptBlock(GetType(),this.ClientID,LazyLoadScriptFilesScript.ToString(),true);
			}
			base.OnPreRender(e);
		}
	}
}
